package com.nhncorp.pleiades.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class FileUtil {
  
  static char[] invalidChars = new char[] {':', '*', '"', '?', '|', '<', '>' };
  
  public static void saveFile(File file, String savePath) {
    FileInputStream inputStream = null;
    ByteArrayOutputStream baos = null;
    try {
      inputStream = new FileInputStream(file);
      baos = new ByteArrayOutputStream();
      OutputStream bos = new FileOutputStream(savePath);
      int bytesRead = 0;
      byte[] buffer = new byte[8192];
      while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
        bos.write(buffer, 0, bytesRead);
      }
    } catch (Exception e) {
    } finally {
      if (inputStream != null)
        try {
          inputStream.close();
        } catch (Exception e) {
        }
      if (baos != null)
        try {
          baos.close();
        } catch (Exception e) {
        }
    }
  }
  public static void saveFile(byte[] file, String savePath) {
    try {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      baos.write(file, 0, file.length);
      baos.close();
    } catch (Exception e) {
    }
  }
  public static void moveFile(String oldFile, String newFile) {
    if (copyFile(oldFile, newFile)) {
      File file = new File(oldFile);
      file.delete();
    }
  }
  public static boolean copyFile(String r_file, String w_file) {
    boolean flag = true;
    FileOutputStream foStream = null;
    String saveDirectory = null;
    File dir = null;
    try {
      int iIndex = w_file.lastIndexOf("/");

      if (iIndex > 0) {
        saveDirectory = w_file.substring(0, (w_file.lastIndexOf("/") + 1));
        dir = new File(replaceStr(saveDirectory, "//", "/"));
        if (!dir.isDirectory()) {
          dir.mkdirs();
        }
      }
      foStream = new FileOutputStream(w_file);
      flag = dumpFile(r_file, foStream);
      foStream.close();
    } catch (Exception ex) {
      try {
        if (foStream != null)
          foStream.close();
      } catch (Exception ex1) {
      }

      flag = false;
    } finally {
      try {
        if (foStream != null)
          foStream.close();
      } catch (Exception ex2) {
      }
    }
    return flag;
  }

  public static boolean dumpFile(String r_file, OutputStream outputstream) {
    byte abyte0[] = new byte[4096];
    boolean flag = true;
    FileInputStream fiStream = null;
    try {
      fiStream = new FileInputStream(r_file);
      int i;
      while ((i = fiStream.read(abyte0)) != -1)
        outputstream.write(abyte0, 0, i);

      fiStream.close();
    } catch (Exception ex) {
      try {
        if (fiStream != null)
          fiStream.close();
      } catch (Exception ex1) {
      }
      flag = false;
    } finally {
      try {
        if (fiStream != null)
          fiStream.close();
      } catch (Exception ex2) {
      }
    }
    return flag;
  }

  public static String replaceStr(String source, String keyStr, String[] toStr) {
    if (source == null)
      return null;
    int startIndex = 0;
    int curIndex = 0;
    int i = 0;
    StringBuffer result = new StringBuffer();
    String specialString = null;

    while ((curIndex = source.indexOf(keyStr, startIndex)) >= 0) {
      if (i < toStr.length)
        specialString = toStr[i++];
      else
        specialString = " ";
      result.append(source.substring(startIndex, curIndex)).append(
          specialString);
      startIndex = curIndex + keyStr.length();
    }
    if (startIndex <= source.length())
      result.append(source.substring(startIndex, source.length()));

    return result.toString();
  }

  public static String replaceStr(String source, String keyStr, String toStr) {
    if (source == null)
      return null;
    int startIndex = 0;
    int curIndex = 0;
    StringBuffer result = new StringBuffer();

    while ((curIndex = source.indexOf(keyStr, startIndex)) >= 0) {
      result.append(source.substring(startIndex, curIndex)).append(toStr);
      startIndex = curIndex + keyStr.length();
    }

    if (startIndex <= source.length())
      result.append(source.substring(startIndex, source.length()));

    return result.toString();

  }
  
  public static void validate (String name) throws IOException {
    for(char entry : invalidChars) {
      if(name.indexOf(entry) >= 0) {
        throw new IOException("Invalid char is included " + name);
      }
    }
  }
  
//  public static boolean isValidFileName(File parent, String name) {
//    
//    if (parent == null || name == null) {
//      return false;
//    }
//    File f = new File(parent, name);
//    if (!f.exists()) {
//      try {
//        boolean b = f.createNewFile();
//        if (b) {
//          f.delete();
//        }
//        if (!f.getParent().equals(parent.getPath())) {
//          return false;
//        }
//        return b;
//      } catch (IOException ioe) {
//        return false;
//      }
//    }
//    return f.getParent().equals(parent.getPath());
//  }

}
